package globalVisionEntertainment.nba;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.sql.Array;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class PlayerPairFrame extends JFrame implements WindowListener
{
	PlayerPairScoreGain[] scoreGains;
	JLabel scoreSumLabel;

	String gameDate;
	private MatchedPlayerControlGroup[] playerPairControlGroups;

	public PlayerPairFrame(PlayerPairScoreGain[] scoreGains)
	{
		super("Matched Players");
		this.scoreGains = scoreGains;

		Container pane = getContentPane();
		pane.setLayout(new GridLayout(0, 9, 20, 20));

		pane.add(createHeadLabel("Home Team Starters"));
		pane.add(createHeadLabel("Average On Court Time"));
		pane.add(createHeadLabel("Proportional Time"));
		pane.add(createHeadLabel("Average score per min"));

		pane.add(createHeadLabel("Visitor Team Starters"));
		pane.add(createHeadLabel("Average On Court Time"));
		pane.add(createHeadLabel("Proportional Time"));
		pane.add(createHeadLabel("Average score per min"));

		pane.add(createHeadLabel("Total Score"));

		// -----------------------------------------

		playerPairControlGroups = new MatchedPlayerControlGroup[scoreGains.length];
		for (int i = 0; i < playerPairControlGroups.length; i++)
		{
			if (i == 5)
			{
				pane.add(createHeadLabel("Home Team Bench"));
				pane.add(createHeadLabel("Average On Court Time"));
				pane.add(createHeadLabel("Proportional Time"));
				pane.add(createHeadLabel("Average score per min"));

				pane.add(createHeadLabel("Visitor Team Bench"));
				pane.add(createHeadLabel("Average On Court Time"));
				pane.add(createHeadLabel("Proportional Time"));
				pane.add(createHeadLabel("Average score per min"));

				pane.add(createHeadLabel("Total Score"));
			}

			playerPairControlGroups[i] = new MatchedPlayerControlGroup();
			JPanel p = new JPanel();

			p.add(playerPairControlGroups[i].homeControls.playerNameLabel);
			pane.add(p);

			p = new JPanel();
			p.add(playerPairControlGroups[i].homeControls.playerAverageTimeLabel);
			pane.add(p);

			p = new JPanel();
			p.add(playerPairControlGroups[i].homeControls.proportionalTimeLabel);
			pane.add(p);

			p = new JPanel();
			p.add(playerPairControlGroups[i].homeControls.averageScorePerMin);
			pane.add(p);

			p = new JPanel();
			p.add(playerPairControlGroups[i].visitorControls.playerNameLabel);
			pane.add(p);

			p = new JPanel();
			p.add(playerPairControlGroups[i].visitorControls.playerAverageTimeLabel);
			pane.add(p);

			p = new JPanel();
			p.add(playerPairControlGroups[i].visitorControls.proportionalTimeLabel);
			pane.add(p);

			p = new JPanel();
			p.add(playerPairControlGroups[i].visitorControls.averageScorePerMin);
			pane.add(p);

			p = new JPanel();
			p.add(playerPairControlGroups[i].pairTotoalScoreLabel);
			pane.add(p);

		}

		JPanel p = new JPanel();
		p.add(createHeadLabel("Predited Score Sum"));
		pane.add(p);

		p = new JPanel();
		scoreSumLabel = new JLabel();
		p.add(scoreSumLabel);
		pane.add(p);

		this.addWindowListener(this);
	}

	private JLabel createHeadLabel(String text)
	{
		JLabel l = new JLabel(text);
		l.setForeground(Color.blue);
		return l;
	}

	@Override
	public void windowOpened(WindowEvent e)
	{
		for (int i = 0; i < playerPairControlGroups.length; i++)
		{
			playerPairControlGroups[i].homeControls.playerNameLabel.setText(scoreGains[i].homeGain.playerName);
			playerPairControlGroups[i].homeControls.playerAverageTimeLabel.setText(String.format("%.2f",
					scoreGains[i].homeGain.averageOnCourtMinutes));
			playerPairControlGroups[i].homeControls.proportionalTimeLabel.setText(String.format("%.2f",
					scoreGains[i].homeGain.proportionalOnCourtMinutes));
			playerPairControlGroups[i].homeControls.averageScorePerMin.setText(String.format("%.4f", scoreGains[i].homeGain.scoreGain));

			playerPairControlGroups[i].visitorControls.playerNameLabel.setText(scoreGains[i].visitorGain.playerName);
			playerPairControlGroups[i].visitorControls.playerAverageTimeLabel.setText(String.format("%.2f",
					scoreGains[i].visitorGain.averageOnCourtMinutes));
			playerPairControlGroups[i].visitorControls.proportionalTimeLabel.setText(String.format("%.2f",
					scoreGains[i].visitorGain.proportionalOnCourtMinutes));
			playerPairControlGroups[i].visitorControls.averageScorePerMin.setText(String
					.format("%.4f", scoreGains[i].visitorGain.scoreGain));

			playerPairControlGroups[i].pairTotoalScoreLabel.setText(String.format("%.2f", scoreGains[i].totalGain));
		}

		pack();
	}

	@Override
	public void windowClosing(WindowEvent e)
	{
	}

	@Override
	public void windowClosed(WindowEvent e)
	{
	}

	@Override
	public void windowIconified(WindowEvent e)
	{
		// TODO 自动生成的方法存根
		//throw new UnsupportedOperationException();
	}

	@Override
	public void windowDeiconified(WindowEvent e)
	{
	}

	@Override
	public void windowActivated(WindowEvent e)
	{

	}

	@Override
	public void windowDeactivated(WindowEvent e)
	{
	}

}

class MatchedPlayerControlGroup
{
	PartyControlGroup homeControls = new PartyControlGroup();
	PartyControlGroup visitorControls = new PartyControlGroup();

	JLabel pairTotoalScoreLabel = new JLabel();

}

class PartyControlGroup
{
	JLabel playerNameLabel = new JLabel();
	JLabel playerAverageTimeLabel = new JLabel();
	JLabel proportionalTimeLabel = new JLabel();
	JLabel averageScorePerMin = new JLabel();
}